﻿using SDF.Core.Infrastructure.DependencyManagement;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Autofac;
using SDF.Core.Configuration;
using SDF.Core.Infrastructure;
using SDF.Plugin.Misc.DynamicEntity.Services;
using SDF.Web.Framework.Mvc;
using SDF.Plugin.Misc.DynamicEntity.Data;
using SDF.Data;
using SDF.Plugin.Misc.DynamicEntity.Domain;
using SDF.Core.Data;
using Autofac.Core;

namespace SDF.Plugin.Misc.DynamicEntity
{
    public class DependencyRegistrar : IDependencyRegistrar
    {
        public int Order
        {
            get
            {
                return 1;
            }
        }

        private const string CONTEXT_NAME = "sdf_dynamicentity_context";

        public void Register(ContainerBuilder builder, ITypeFinder typeFinder, SDFConfig config)
        {
            builder.RegisterType<DynamicEntityService>().As<IDynamicEntityService>().InstancePerLifetimeScope();

            //data context
            this.RegisterPluginDataContext<DataContextDynamicEntity>(builder, CONTEXT_NAME);

            //override required repository with our custom context
            builder.RegisterType<EfRepository<DynamicEntityHeader>>()
               .As<IRepository<DynamicEntityHeader>>()
               .WithParameter(ResolvedParameter.ForNamed<IDbContext>(CONTEXT_NAME))
               .InstancePerLifetimeScope();

            builder.RegisterType<EfRepository<DynamicEntityItem>>()
               .As<IRepository<DynamicEntityItem>>()
               .WithParameter(ResolvedParameter.ForNamed<IDbContext>(CONTEXT_NAME))
               .InstancePerLifetimeScope();

        }
    }
}